package com.five.calendar.ui.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.transition.Transition;
import android.transition.TransitionInflater;
import android.view.View;
import android.view.Window;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.five.calendar.R;
import com.five.calendar.base.BaseActivity;
import com.five.calendar.ui.adapter.YearAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @desc:
 * @auther:duwei
 * @date:2017/9/7
 */

public class YearActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R.id.calendar_year)
    RecyclerView mRecyclerView;

    YearAdapter mYearAdapter;

    List<YearAdapter.YearBean> mData = new ArrayList<>();

    int mCurrentYear = 2017;
    int mFirstYear = mCurrentYear, mLastYear = mCurrentYear;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Transition explode = TransitionInflater.from(this).inflateTransition(R.transition.explode);
            getWindow().setExitTransition(explode);
            getWindow().setEnterTransition(explode);
            getWindow().setReenterTransition(explode);
            getWindow().setReturnTransition(explode);
        }
        setContentView(R.layout.calendar_year);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        mYearAdapter = new YearAdapter(R.layout.calendar_year_item);
        mYearAdapter.setUpFetchEnable(true);
        mYearAdapter.setOnLoadMoreListener(this, mRecyclerView);
        mYearAdapter.setMonthClickCallBack(new YearAdapter.OnMonthClickCallBack() {
            @Override
            public void onMonthClick(int year, int month) {
                Intent intent = getIntent();
                intent.putExtra("year", year);
                intent.putExtra("month", month);
                setResult(0, intent);
                finish();
            }
        });
        YearAdapter.YearBean yearBean = new YearAdapter.YearBean();
        yearBean.setYear(mCurrentYear);
        mData.add(yearBean);
        mYearAdapter.setNewData(mData);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mYearAdapter);
        mYearAdapter.setUpFetchListener(new BaseQuickAdapter.UpFetchListener() {
            @Override
            public void onUpFetch() {
                startUpFetch();
            }
        });
    }

    private int count;

    private void startUpFetch() {
        count++;
        /**
         * set fetching on when start network request.
         */
        mYearAdapter.setUpFetching(true);
        /**
         * get data from internet.
         */
        mRecyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                YearAdapter.YearBean yearBean = new YearAdapter.YearBean();
                mFirstYear = --mFirstYear;
                yearBean.setYear(mFirstYear);
                mYearAdapter.addData(0, yearBean);
                /**
                 * set fetching off when network request ends.
                 */
                mYearAdapter.setUpFetching(false);
                /**
                 * set fetch enable false when you don't need anymore.
                 */
                if (count > 5) {
                    mYearAdapter.setUpFetchEnable(false);
                }
            }
        }, 300);
    }

    @Override
    public void onLoadMoreRequested() {
        YearAdapter.YearBean yearBean = new YearAdapter.YearBean();
        mLastYear = ++mLastYear;
        yearBean.setYear(mLastYear);
        mYearAdapter.addData(yearBean);
        mYearAdapter.loadMoreComplete();
    }
}
